﻿using System;
using System.Diagnostics.Contracts;
using System.Linq;

namespace BTerrariaMod.Bards
{

    /// <summary>
    /// Does something.
    /// </summary>
    public class WorldBard
    {
        /// <summary>
        /// The action for all output.
        /// </summary>
        private readonly Action<string> _output;

        /// <summary>
        /// Initializes a new instance of the <see cref="WorldBard"/> class.
        /// </summary>
        /// <param name="output">The output.</param>
        public WorldBard(Action<string> output)
        {
            Contract.Requires(output != null);
            this._output = output;
        }

        /// <summary>
        /// Announces the spawn.
        /// </summary>
        /// <param name="x">The x.</param>
        /// <param name="y">The y.</param>
        public void AnnounceSpawn(int x, int y)
        {
            this._output("Announce spawn called:" + x.ToString() + "," + y);
        }

        /// <summary>
        /// Announces the orbs.
        /// </summary>
        public void AnnounceOrbs(Terraria.Tile[,] tiles)
        {
            var foundOrbTiles = new System.Collections.Generic.List<Terraria.Tile>();
            foreach (var t in tiles)
                if (t.type == Mappings.Tile.ShadowOrbType)
                    foundOrbTiles.Add(t);
            
            var q = from f1 in foundOrbTiles //shadow orb is 2x2 don't count duplicates
                    join f2 in foundOrbTiles on new{frameX=(short)(f1.frameX+1),f1.frameY} equals new{f2.frameX,f2.frameY}
                    join f3 in foundOrbTiles on new { frameX = f1.frameX, frameY = (short)(f1.frameY + 1 )} equals new { f3.frameX, f3.frameY }
                    join f4 in foundOrbTiles on new { frameX = (short)(f1.frameX + 1), frameY = (short)(f1.frameY + 1) } equals new { f4.frameX, f4.frameY }
                    select f1;
            var orbs = q.Count();
            _output("found " + orbs + " orbs");
        }
    }
}
